import 'package:flutter/material.dart';

class AnimationDemo extends StatelessWidget {
  const AnimationDemo({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('AnimationDemo'),
        elevation: 0.0,
      ),
      body: AnimationDemoHome(),
    );
  }
}

class AnimationDemoHome extends StatefulWidget {
  AnimationDemoHome({Key key}) : super(key: key);

  _AnimationDemoHomeState createState() => _AnimationDemoHomeState();
}

class _AnimationDemoHomeState extends State<AnimationDemoHome>
    with TickerProviderStateMixin {
  AnimationController _animationControllerdemo;
  Animation animation;
  Animation animationColor;
  // 动画曲线
  CurvedAnimation curve;
  @override
  void initState() {
    super.initState();
    _animationControllerdemo = AnimationController(
      // lowerBound: 32.0,
      // upperBound: 100.0,
      // value: 32.0,
      duration: Duration(milliseconds: 2000),
      // vsync传入是防止动画离屏之后继续消耗资源。
      vsync: this,
    );
    _animationControllerdemo.addListener(() {
      setState(() {});
      print('${_animationControllerdemo.value}');
    });
    _animationControllerdemo.addStatusListener((AnimationStatus status) {
      print('status: $status');
    });
    curve = CurvedAnimation(
        parent: _animationControllerdemo, curve: Curves.bounceOut);
    animation = Tween(begin: 32.0, end: 100.0).animate(curve);
    animationColor =
        ColorTween(begin: Colors.red, end: Colors.black).animate(curve);
    // _animationControllerdemo.forward();
  }

  @override
  void dispose() {
    _animationControllerdemo.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: IconButton(
          icon: Icon(Icons.favorite),
          iconSize: animation.value,
          color: animationColor.value,
          onPressed: () {
            switch (_animationControllerdemo.status) {
              case AnimationStatus.completed:
                _animationControllerdemo.reverse();
                break;
              default:
                _animationControllerdemo.forward();
            }
          },
        ),
      ),
    );
  }
}
